<?php

namespace App\Admin\Models;

use App\Admin\Models\BaseModel;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\MessageBag;
use Ixudra\Curl\Facades\Curl;
use Encore\Admin\Facades\Admin;
use App\Services\Store;
use App\Services\Common;
use DB;

class StoreCardsLog extends BaseModel
{   
    //指定主键
    protected $primaryKey = 'sclId';
    
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
    {
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);
        $smcId = Request::get('smcId', false);
        $auCode = Request::get('auCode', false);
        $muCode = Request::get('muCode', false);

        $start = ($page-1)*$perPage;

		///////获取数据//////////////////////////////////////////////////////////////////////////////////////
		$sId = Admin::user()->sId;

		if ($smcId === false){
			if ($auCode){
				$list = DB::table('store_cards_log')
					->select(['store_cards_log.*', 'admin_users.name', 'member_users.muNickName', 'store_goods.sgServiceMoney'])
					->leftJoin('admin_users', 'store_cards_log.auCode', '=', 'admin_users.auCode')
					->leftJoin('store_goods', 'store_cards_log.sgId', '=', 'store_goods.sgId')
					->leftJoin('member_users', 'store_cards_log.muCode', '=', 'member_users.muCode')
					->where('store_cards_log.sId', $sId)
					->where('store_cards_log.auCode', '=', $auCode)
					->orderBy('sclId', 'DESC')
					->offset($start)
        			->limit($perPage)
					->get();
			}else if ($muCode){
				$list = DB::table('store_cards_log')
					->select(['store_cards_log.*', 'admin_users.name', 'member_users.muNickName', 'store_goods.sgServiceMoney'])
					->leftJoin('admin_users', 'store_cards_log.auCode', '=', 'admin_users.auCode')
					->leftJoin('member_users', 'store_cards_log.muCode', '=', 'member_users.muCode')
					->leftJoin('store_goods', 'store_cards_log.sgId', '=', 'store_goods.sgId')
					->where('store_cards_log.sId', $sId)
					->where('store_cards_log.muCode', $muCode)
					->orderBy('store_cards_log.sclId', 'DESC')
					->offset($start)
        			->limit($perPage)
					->get();
			}else{
				$list = DB::table('store_cards_log')
					->select(['store_cards_log.*', 'admin_users.name', 'member_users.muNickName', 'store_goods.sgServiceMoney'])
					->leftJoin('admin_users', 'store_cards_log.auCode', '=', 'admin_users.auCode')
					->leftJoin('store_goods', 'store_cards_log.sgId', '=', 'store_goods.sgId')
					->leftJoin('member_users', 'store_cards_log.muCode', '=', 'member_users.muCode')
					->where('store_cards_log.sId', $sId)
					->orderBy('sclId', 'DESC')
					->offset($start)
        			->limit($perPage)
					->get();
			}
		}else{
			$list = DB::table('store_cards_log')
					->select(['store_cards_log.*', 'admin_users.name', 'member_users.muNickName', 'store_goods.sgServiceMoney'])
					->leftJoin('admin_users', 'store_cards_log.auCode', '=', 'admin_users.auCode')
					->leftJoin('store_goods', 'store_cards_log.sgId', '=', 'store_goods.sgId')
					->leftJoin('member_users', 'store_cards_log.muCode', '=', 'member_users.muCode')
					->where('smcId', $smcId)
					->orderBy('smcId', 'DESC')
					->get();
		}
		
		$list = json_decode(json_encode($list), true);
		////////获取数据/////////////////////////////////////////////////////////////////////////////////////

		$total = count($list);
		
        extract($list);

        $tags = static::hydrate($list);

        $paginator = new LengthAwarePaginator($tags, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
    
    /**
     * Add a basic where clause to the query.
     *
     * @param  string|array|\Closure  $column
     * @param  mixed   $operator
     * @param  mixed   $value
     * @param  string  $boolean
     * @return $this
     */
    public function where($column, $operator = null, $value = null, $boolean = 'and'){

    	return $this;
    }
}
